//hooks封装、
import { useIntersectionObserver } from '@vueuse/core'
import { ref } from 'vue'

export const useLazyData = (apiFn) => {
  //观察的对象
  const target = ref(null)
  //不同的api函数
  const result = ref([])

  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }], observerElement) => {
      if (isIntersecting) {
        stop()
        //调用api获取数据
        apiFn().then((data) => {
          result.value = data.result
        })
      }
    },
    //立即获取
    {
      threshold: 0
    }
  )
  return { target, result }
}
